<template>
    <NavBar title="个人主页" />
    <div class="person-home">
        <div class="person-card-wrap">
            <div class="person-card">
                <van-image lazy-load round :src="baseUrl + info.avatar" style="width: 80px;height: 80px;" />
                <h3>医师姓名:{{ info.name ? info.name : '暂无' }}</h3>
                <p>职称:{{ info.title ? info.title : '暂无' }}</p>
                <p>医疗机构名称:{{ info.bus_title ? info.bus_title : '暂无' }}</p>
                <div>
                    擅长领域:
                    <span v-if="!info.tags">暂无</span>
                    <van-space>
                        <van-tag round plain type="primary" v-for="(item, index) in info.tags" :key="index">
                            {{ item }}
                        </van-tag>
                    </van-space>
                </div>
                <van-divider></van-divider>
                <ul class="my-row2">
                    <li>
                        <div>0</div>
                        <div>获赞</div>
                    </li>
                    <li>
                        <div>0</div>
                        <div>粉丝</div>
                    </li>
                    <li>
                        <div>0</div>
                        <div>关注</div>
                    </li>
                    <li v-if="role_id != 1">
                        <van-button round size="small" type="primary"
                            :to="{ name: 'appoint', query: { doctorid: id } }">
                            立即预约
                        </van-button>
                    </li>
                    <li v-else>
                        <van-button round size="small" type="primary"
                            :to="{ name: 'businessHome', query: { id: bus_id } }">
                            医疗机构主页
                        </van-button>
                    </li>
                </ul>
            </div>
        </div>
        <div style="height: 160px;"></div>
        <!-- 个人介绍: -->
        <div class="introduce">
            <van-divider><b>个人介绍</b></van-divider>
            <van-empty description="暂无医师介绍" v-if="!info.introduce"></van-empty>
            <div v-html="info.introduce"></div>
        </div>

        <div class="comment">
            <div v-if="token && role_id === 2">
                <div class="panel-title">患者评价</div>
                <van-empty description="暂无评论数据" v-if="commentList.length === 0"></van-empty>
                <van-form @submit="onSubmit">
                    <van-field name="rate" :rules="[
                        { required: true, message: '请选择评价等级' }
                    ]">
                        <template #input>
                            <van-rate v-model="rate" :size="25" color="#ffd21e" void-icon="star" void-color="#eee" />
                        </template>
                    </van-field>
                    <van-field v-model="content" name="content" :rules="[
                        { required: true, message: '请输入评论内容' }
                    ]" border placeholder="请输入评论内容"></van-field>
                    <van-button native-type="submit" size="small" plain block type="primary" round>发布评论</van-button>
                </van-form>
            </div>
            <van-divider></van-divider>
            <ul>
                <li v-for="item in commentList" :key="item.id">
                    <strong>{{ item.patient }}:</strong>
                    <van-rate readonly v-model="item.rate" :size="25" color="#ffd21e" void-icon="star"
                        void-color="#eee" />
                    <p class="small">
                        {{ item.content }}
                    </p>
                    <p class="small">
                        <!-- 类型断言 -->
                        {{ dateFmt(item.create_time) }}
                    </p>
                    <van-tag @click="del(item.id)" v-if="patientid === item.patientid" round plain
                        type="danger">删除</van-tag>
                </li>
            </ul>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { dateFmt } from '@/utils/util';
import { DoctorType, CommentType } from '@/types';
import { useQuery } from '@/utils/hooks';
import { getDoctorDetail, getCommentlist, commentAdd, commentRemove } from '@/utils/api';
import { ref, Ref, onMounted } from 'vue';
import { showToast, showConfirmDialog } from 'vant';
const { id: patientid, token, role_id, bus_id } = JSON.parse(localStorage.getItem('userLoginInfo') || '{}');
const { id } = useQuery();
const info: Ref<DoctorType> = ref({});
const commentList: Ref<CommentType[]> = ref([]);
const getCmtList = async () => {
    const { data: { data: cmtList } } = await getCommentlist(id);
    commentList.value = cmtList;
}

onMounted(async () => {
    const { data: { data } } = await getDoctorDetail(id);
    info.value = data;
    getCmtList();
});
// 评价等级
const rate: Ref<number> = ref(5);
const content = ref('');
const onSubmit = async (values: any) => {
    const payload = {
        doctorid: id,
        patientid,
        ...values
    }
    const { data } = await commentAdd(payload);
    showToast(data.msg);
    if (data.code === 200) {
        // 重新获取评论列表
        getCmtList();
    }


}
const del = (id: number | undefined) => {
    showConfirmDialog({
        title: '提示',
        message:
            '确认删除?',
    })
        .then(async () => {
            const { data } = await commentRemove(id);
            showToast(data.msg);
            if (data.code === 200) {
                const index = commentList.value.findIndex(item => item.id === id);
                if (index > -1) {
                    commentList.value.splice(index, 1);
                }
            }
        })
        .catch(() => null);
}
</script>

<style lang="scss" scoped>
.person-card-wrap {
    position: relative;
    background: #39A9ED;
    border-radius: 0 0 50% 50%;
    height: 240px;

    .person-card {
        position: absolute;
        width: 80%;
        top: 50px;
        left: 50%;
        transform: translateX(-50%);
        background-color: #fff;
        text-align: center;
        padding-top: 10px;

        .my-row2 {
            display: flex;
            margin: 10px 0;
            align-items: center;
            padding: 10px;

            li {
                flex: 1;
                text-align: center;
            }

            .person-home {
                color: #333;
                text-decoration: none;
                border: 1px solid #EEE;
                border-radius: 20px;
                padding: 2px 4px;
            }
        }
    }
}

.introduce {
    margin: 20px;
    padding: 20px;
}

.comment {
    margin: 10px 20px;
}

.small {
    font-size: 14px;
    color: #666;
}
</style>